package com.shaoshaohuo.app.view;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.listener.IMarketConditionListener;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Price;
import com.shaoshaohuo.app.entity.PriceEntity;
import com.shaoshaohuo.app.framework.IViewFramework;
import com.shaoshaohuo.app.framework.log.LogTags;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.utils.StringUtil;

/**
 * 首页市场行情
 * @author YinSF
 * @createDate 2015-11-8
 */
public class TodayMarketConditionView extends LinearLayout implements IViewFramework, OnClickListener, IMarketConditionListener {
	private TextView tvShichang;
	private TextView tvChandi;
	private View baselineLeft;
	private View baselineRight;
	private ViewFlipper flipper;
	private boolean isMarketSelected = false;
	private PriceEntity peMarket;
	private PriceEntity peOriginPlace;
	private PriceEntity currentPriceEntity;
	private static final int MSG_UPDATE = 1;
	
	private static final String TYPE_MARKET = "1";//市场
	private static final String TYPE_ORIGIN_PLACE = "2";//产地
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_UPDATE:
				if (isMarketSelected) {
					resetFlipperSubviews(peMarket);
				} else {
					resetFlipperSubviews(peOriginPlace);
				}
				break;
			default:
				break;
			}
		}
		
	};
	
	public TodayMarketConditionView(Context context) {
		super(context);
		init();
	}

	public TodayMarketConditionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	@Override
	public void init() {
		inflate(getContext(), R.layout.view_today_market_condition, this);
		initView();
		initData();
		initListener();
		initAnimation();
	}

	/**
	 * 设置上下滚动动画
	 */
	private void initAnimation() {
		flipper.setInAnimation(AnimationUtils.loadAnimation(getContext(),  
                R.anim.slide_bottom_in));  
		flipper.setOutAnimation(AnimationUtils.loadAnimation(getContext(),  
                R.anim.slide_top_out));
		flipper.startFlipping();
		
	}

	@Override
	public void initView() {
		tvShichang = (TextView) findViewById(R.id.tv_tab_shichang);
		tvChandi = (TextView) findViewById(R.id.tv_tab_chandi);
		baselineLeft = (View) findViewById(R.id.view_baseline_left);
		baselineRight = (View) findViewById(R.id.view_baseline_right);
		flipper = (ViewFlipper) findViewById(R.id.flipper);
	}

	@Override
	public void initData() {
//		TodayMarketConditionService.addListener(this);
//		TodayMarketConditionService.updateMarket();
//		TodayMarketConditionService.updateOriginPlace();
		initFlipperData();
	}

	@Override
	public void initListener() {
		tvShichang.setOnClickListener(this);
		tvChandi.setOnClickListener(this);
		tvChandi.setEnabled(false);
		tvShichang.setEnabled(true);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.tv_tab_chandi:
			baselineLeft.setVisibility(View.VISIBLE);
			baselineRight.setVisibility(View.INVISIBLE);
			tvChandi.setEnabled(false);
			tvShichang.setEnabled(true);
			if (isMarketSelected) {
				initFlipperData();
			}
			isMarketSelected = false;
			break;
		case R.id.tv_tab_shichang:
			baselineLeft.setVisibility(View.INVISIBLE);
			baselineRight.setVisibility(View.VISIBLE);
			tvChandi.setEnabled(true);
			tvShichang.setEnabled(false);
			if (!isMarketSelected) {
				initFlipperData();
			}
			isMarketSelected = true;
			break;
		default:
			break;
		}
	}
	
	
	
	public class MarketCoonditionItem  {
		private String name;
		private String catname;
		private String price;
		private String changeType;
		private String changePrice;
		private String address;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getCatname() {
			return catname;
		}
		public void setCatname(String catname) {
			this.catname = catname;
		}
		public String getPrice() {
			return price;
		}
		public void setPrice(String price) {
			this.price = price;
		}
		public String getChangeType() {
			return changeType;
		}
		public void setChangeType(String changeType) {
			this.changeType = changeType;
		}
		public String getChangePrice() {
			return changePrice;
		}
		public void setChangePrice(String changePrice) {
			this.changePrice = changePrice;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		
	}



	@Override
	public void updateMarket(PriceEntity pe) {
		peMarket = pe;
		initFlipperData();
	}

	private void initFlipperData() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				RequestService.getInstance().marketPrice(getContext(), TYPE_MARKET,null, null, null, null, null, null, PriceEntity.class, new RequestListener() {
					
					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						if (resultData.isOk()) {
							Log.i(LogTags.TAG_MARKET_CONDITION, "service market info received");
							peMarket = (PriceEntity) resultData;
							Message msg = new Message();
							msg.what = MSG_UPDATE;
							handler.sendMessage(msg);
//							updateMarket();
						}
					}
					
					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
					}
				});
				RequestService.getInstance().marketPrice(getContext(), TYPE_ORIGIN_PLACE,null,null, null, null, null, null, PriceEntity.class, new RequestListener() {
					
					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						if (resultData.isOk()) {
							Log.i(LogTags.TAG_MARKET_CONDITION, "service orign place info received");
							peOriginPlace = (PriceEntity) resultData;
							Message msg = new Message();
							msg.what = MSG_UPDATE;
							handler.sendMessage(msg);
//							updateOriginPlace();
						}
					}
					
					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
					}
				});
				
			}
			
		}).start();
	}
	
	private void resetFlipperSubviews(PriceEntity pe) {
		if (pe == null || pe.getData() == null) {
			flipper.addView(LayoutInflater.from(getContext()).inflate(R.layout.view_flipper_home, null));
			flipper.addView(LayoutInflater.from(getContext()).inflate(R.layout.view_flipper_home, null));
			flipper.addView(LayoutInflater.from(getContext()).inflate(R.layout.view_flipper_home, null));
			return;
		}
		currentPriceEntity = pe;
		flipper.removeAllViews();
		
		List<Price> priceList = currentPriceEntity.getData().getList();
		View view = null;
		ItemViewHolder holder;
		for (Price p : priceList) {
			/*if (view == null) {
				view = LayoutInflater.from(getContext()).inflate(R.layout.view_flipper_home, null);
				holder = new ItemViewHolder();
				holder.nameView = (TextView) view.findViewById(R.id.tmc_name);
				holder.catnameView = (TextView) view.findViewById(R.id.tmc_catname);
				holder.priceView = (TextView) view.findViewById(R.id.tmc_price);
				holder.priceUnitView = (TextView) view.findViewById(R.id.tmc_price_unit);
				holder.changeTypeView = (ImageView) view.findViewById(R.id.tmc_change_type);
				holder.changePriceView = (TextView) view.findViewById(R.id.tmc_change_price);
				holder.addressView = (TextView) view.findViewById(R.id.tmc_address);
				view.setTag(holder);
			} else {
				view = view;
				holder = (ItemViewHolder) view.getTag();
			}
					holder.nameView.setText(p.getProductName());
					holder.catnameView.setText(p.getProductType());
					holder.priceView.setText(isMarketSelected ? p.getLowPrice() : p.getHighPrice());
			String unitString = "元";
			if (!StringUtil.isEmpty(p.getUnit())) {
				unitString += "/" + p.getUnit();
			}
			holder.priceUnitView.setText(unitString);
			if ("1".equals(p.getPriceType())) {
				holder.changeTypeView.setImageResource(R.drawable.arrow_up_ad);
				holder.changePriceView.setText(p.getMoney() + "元");
			} else if ("2".equals(p.getPriceType())) {
				holder.changeTypeView.setImageResource(R.drawable.arrow_down_ad);
				holder.changePriceView.setText(p.getMoney() + "元");
			} else if ("3".equals(p.getPriceType())) {
				holder.changeTypeView.setImageResource(R.drawable.icon_fair_ad);//TODO 需要替换成持平的图标
				holder.changePriceView.setText("");
			}
					holder.addressView.setText(p.getAddressInfo());*/
			view = LayoutInflater.from(getContext()).inflate(R.layout.view_flipper_home, null);
			TextView nameView = (TextView) view.findViewById(R.id.tmc_name);
			nameView.setText(p.getProductName());
			TextView catnameView = (TextView) view.findViewById(R.id.tmc_catname);
			catnameView.setText(p.getProductType());
			TextView priceView = (TextView) view.findViewById(R.id.tmc_price);
			priceView.setText(isMarketSelected ? p.getLowPrice() : p.getHighPrice());
			TextView priceUnitView = (TextView) view.findViewById(R.id.tmc_price_unit);
			String unitString = "元";
			if (!StringUtil.isEmpty(p.getUnit())) {
				unitString += "/" + p.getUnit();
			}
			priceUnitView.setText(unitString);
			ImageView changeTypeView = (ImageView) view.findViewById(R.id.tmc_change_type);
			TextView changePriceView = (TextView) view.findViewById(R.id.tmc_change_price);
			if ("1".equals(p.getPriceType())) {
				changeTypeView.setImageResource(R.drawable.arrow_up_ad);
				changePriceView.setText(p.getMoney() + "元");
			} else if ("2".equals(p.getPriceType())) {
				changeTypeView.setImageResource(R.drawable.arrow_down_ad);
				changePriceView.setText(p.getMoney() + "元");
			} else if ("3".equals(p.getPriceType())) {
				changeTypeView.setImageResource(R.drawable.icon_fair_ad);//TODO 需要替换成持平的图标
				changePriceView.setText("");
			}
			TextView addressView = (TextView) view.findViewById(R.id.tmc_address);
			addressView.setText(p.getAddressInfo());
			flipper.addView(view);
		}
	}

	private class ItemViewHolder {
		TextView nameView;
		TextView catnameView;
		TextView priceView;
		TextView priceUnitView;
		ImageView changeTypeView;
		TextView changePriceView;
		TextView addressView;
	}
	
	@Override
	public void updateOriginPlace(PriceEntity pe) {
		peOriginPlace = pe;
		initFlipperData();
	}
}
